
const db = require('../serve/db.js')
const connect = require('../utils/connect')
const token = require('../utils/token')

function result(status,data,msg){
    this.status = status;
    this.data = data || {};
    this.msg = msg;
}
async function Commit(req,res){
  let data = {
    token:req.body.token ||1,
    text:req.body.text || '',
    content: req.body.content || '',
    tags:req.body.tags ||[],
    CreateTime:Date.parse(new Date())/1000,
    hash:Math.random().toString(36).substr(2,10)
  };
  for(var i in data){
    if(!data[i]){
      res.send(new result(110,'',i+'未填写！'));
      return
    }
  }
  let dbc = await connect();
  let tok = await token(dbc,'token',{token:data.token})
  if(tok){
    let usr = await dbc.db('mongo').collection('member').findOne({uid:tok.uid})
    data.creater = usr.member;
    data.uid     = usr.uid;
    data.head    = usr.head;
    delete data.token;
    db.insert('artical',data);
    res.send(new result(200,data,'ok'))
  }else{
    dbc.close()
    res.send(new result(101,{},'未登录'))
  }
  // db.insert('artical',data);
}

module.exports = Commit